#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int n;
struct Stu{
    string name,gender,id;
    int grade;
};
vector<Stu> m,f;

int cmp(Stu &s1,Stu &s2){
    return s1.grade<s2.grade;
}

signed main()
{
    cin>>n;
    for(int i=0;i<n;i++){
        string a,b,c;
        int d;
        cin>>a>>b>>c>>d;
        if(b=="M")    m.push_back({a,b,c,d});
        else    f.push_back({a,b,c,d});
    }
    sort(m.begin(),m.end(),cmp);
    sort(f.begin(),f.end(),cmp);
    if(f.empty())    cout<<"Absent\n";
    else    cout<<f[f.size()-1].name<<" "<<f[f.size()-1].id<<"\n";
    if(m.empty())    cout<<"Absent\n";
    else    cout<<m[0].name<<" "<<m[0].id<<"\n";
    if(f.empty()||m.empty())    cout<<"NA\n";
    else    cout<<abs(f[f.size()-1].grade-m[0].grade)<<"\n";
    return 0;
}
